package co.yixiang.mall.biz.user.service.impl;

import co.yixiang.mall.biz.activity.service.YxUserExtractService;
import co.yixiang.mall.biz.order.service.YxStoreOrderService;
import co.yixiang.mall.biz.rebateFreeze.service.UserRebateFreezeService;
import co.yixiang.mall.biz.transfer.service.UserOutLogService;
import co.yixiang.mall.biz.user.service.UserBalanceService;
import co.yixiang.mall.biz.user.service.YxUserRechargeService;
import co.yixiang.mall.common.activity.domain.YxUserExtract;
import co.yixiang.mall.common.mall.domain.YxUserRecharge;
import co.yixiang.mall.common.order.domain.YxStoreOrder;
import co.yixiang.mall.common.rebateFreeze.domain.UserRebateFreeze;
import co.yixiang.mall.common.transfer.domain.UserOutLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wangxueliang
 * @Date 2021/3/19
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserBalanceServiceImpl implements UserBalanceService {
    @Autowired
    private YxUserExtractService yxUserExtractService;
    @Autowired
    private YxUserRechargeService yxUserRechargeService;
    @Autowired
    private YxStoreOrderService yxStoreOrderService;
    @Autowired
    private UserRebateFreezeService userRebateFreezeService;
    @Autowired
    private UserOutLogService userOutLogService;

    @Override
    public Map<String, Map<String, String>> balanceDetail(String uid) {
        Map<String, Map<String, String>> returnMap = new HashMap<>();
        Map<String, String> extractMap = new TreeMap<>();
        Map<String, String> rechargeMap = new HashMap<>();
        Map<String, String> orderMap = new HashMap<>();
        Map<String, String> rebateFreezeMap = new HashMap<>();
        Map<String, String> transferMap = new HashMap<>();

        if (uid == null || uid == "") {
            returnMap.put("用户为空", null);
            return returnMap;
        }
        try {
            //提现记录
            List<YxUserExtract> yxUserExtractList = yxUserExtractService.lambdaQuery().eq(YxUserExtract::getUid, uid).in(YxUserExtract::getStatus, 0, 1).orderByDesc(YxUserExtract::getCreateTime).list();
            for (YxUserExtract yxUserExtract : yxUserExtractList) {
                extractMap.put(transition(yxUserExtract.getCreateTime().toString()), "提现:" + yxUserExtract.getExtractPrice());
            }
            returnMap.put("提现明细", extractMap);

            //充值记录
            List<YxUserRecharge> rechargeList = yxUserRechargeService.lambdaQuery().eq(YxUserRecharge::getUid, uid).orderByDesc(YxUserRecharge::getCreateTime).list();
            for (YxUserRecharge yxUserRecharge : rechargeList) {
                rechargeMap.put(transition(yxUserRecharge.getCreateTime().toString()), "充值：" + yxUserRecharge.getPrice().add(yxUserRecharge.getGivePrice()));
            }
            returnMap.put("充值明细", rechargeMap);

            //用户订单记录
            List<YxStoreOrder> storeOrderList = yxStoreOrderService.lambdaQuery().eq(YxStoreOrder::getUid, uid).eq(YxStoreOrder::getPayType, "1").orderByDesc(YxStoreOrder::getCreateTime).list();
            for (YxStoreOrder yxStoreOrder : storeOrderList) {
                if (yxStoreOrder.getRefundStatus() == 2) {
                    orderMap.put(transition(yxStoreOrder.getRefundReasonTime().toString()), "退款：" + yxStoreOrder.getRefundPrice());
                } else {
                    orderMap.put(transition(yxStoreOrder.getPayTime().toString()), "消费：" + yxStoreOrder.getPayPrice());
                }
            }
            returnMap.put("订单明细", orderMap);

            //冻结金额明细
            List<UserRebateFreeze> rebateFreezeList = userRebateFreezeService.lambdaQuery().eq(UserRebateFreeze::getUserId, uid).notIn(UserRebateFreeze::getState, 3).orderByDesc(UserRebateFreeze::getStartTime).list();
            for (UserRebateFreeze userRebateFreeze : rebateFreezeList) {
                if (userRebateFreeze.getState() == 1) {
                    rebateFreezeMap.put(userRebateFreeze.getStartTime().toString(), "冻结：" + userRebateFreeze.getMoney().toString());
                } else {
                    rebateFreezeMap.put(userRebateFreeze.getStartTime().toString(), "入账：" + userRebateFreeze.getMoney());
                }
            }
            returnMap.put("冻结明细", rebateFreezeMap);

            //转账明细
            List<UserOutLog> outLogList = userOutLogService.lambdaQuery().eq(UserOutLog::getUserid, Integer.parseInt(uid)).or().eq(UserOutLog::getOutUserid, Integer.parseInt(uid)).orderByDesc(UserOutLog::getOutDate).list();
            for (UserOutLog userOutLog : outLogList) {
                if (userOutLog.getUserid() == Integer.parseInt(uid)) {
                    transferMap.put(transition(userOutLog.getOutDate().toString()),"转出：" + userOutLog.getOutMoney());
                } else {
                    transferMap.put(transition(userOutLog.getOutDate().toString()),"转入：" + userOutLog.getOutMoney());
                }
            }
            returnMap.put("转账记录", transferMap);
            return returnMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnMap;
    }


    /**
     * 时间格式转换
     * @param str
     * @return
     */
    public static String transition(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

}
